//异步请求JSON数据实现
function GetJsonDataAsync(url, params) {
    const {
        hostType = 'https',
        method = 'get'
    } = params || {};
    const httpHostType = require(hostType === 'https' ? 'https' : 'http');
    return new Promise((resolve, reject) => {
        httpHostType[method](url, (response) => {
            let data = '';
            //数据正在接收中...
            response.on('data', (chunk) => {
                data += chunk;
            });
            //数据接收完成
            response.on('end', () => {
                resolve(data);//数据接收完成
            });

        }).on('error', (error) => {
            reject(new Error(error.message));
        });
    });
}

function getAllHeatMap() {
    return new Promise((resolve, reject) => {
        const host = 'https://gitee.com';
        const path = '/liu_zhaojun/public/raw/master/mock/data/heatMap.json';
        GetJsonDataAsync(host + path)
            .then((value) => {
                if (typeof value === 'string') {
                    const valueObj = JSON.parse(value);
                    resolve(valueObj);
                }
            })
            .catch(e => {
                reject(e);
            });
    });
    //异步调用
}

module.exports = {
    GetJsonDataAsync,
    getAllHeatMap,
};
